package org.aaa.cloudagent;
import java.net.*;  
import java.io.*;  

public class FileServThread implements Runnable
{

	  private Socket sock;  
	  FileServThread(Socket sock)  
	  {  
	    this.sock = sock;  
	  }  
	  static int port = 5544;
	  
	  public void run()  
	  {  
	    String ip = sock.getInetAddress().getHostAddress();   //��ȡ�ͻ���ip  
	    try  
	    {  
	      Log.op("client ip: "+ip);  
	      InputStream sockIn= sock.getInputStream();//����socket������,���տͻ��˵���Ϣ  
	      File file =getClientFileName(sockIn);     //����ͬ���ļ�  
	      if(file==null)  
	      {  
	        writeOutInfo(sock,"writeOutInfo error!");  
	        sock.close();              return;  
	      }  
	       
	      FileOutputStream fos= new FileOutputStream(file); //��4д��Ӳ��  
	      byte[] bufFile = new byte[1024*1024];   //������ݵĻ���  
	      int len=0;  
	      while(true)  
	      {  
	        len=sockIn.read(bufFile); //�������  
	        if(len!=-1)  
	        {  
	          fos.write(bufFile,0,len); //д��Ӳ���ļ�  
	        }  
	        else  
	        {  
	          break;  
	        }  
	      }  
	      
	      
	      ++port;
	      writeOutInfo(sock,"ok! URL : rtsp://IP:"+ port + "/" + file.getName());   //�ļ����ճɹ����ͻ��˷�!һ����Ϣ  
	      
	      Log.op("get file over over!"+System.getProperty("line.separator"));  //����˴�ӡһ��  
	      fos.close();  
	      sock.close();  
	      
	      JavaShellUtil  ShellExec= new JavaShellUtil();
	      
	      //put
	      FileDir  filedir = new FileDir(file.getName());
	      String savedir = filedir.GetSaveDir();
	      String shellCmd = "mv " + "/tmp/"+file.getName()+" "+ savedir + file.getName();
	      ShellExec.executeShell(shellCmd);	
	      
	      //play	      
	      
	      shellCmd = "/home/hadoop/startvideo.sh "+ savedir+ file.getName() + " " + file.getName() + " " + port;
	      //shellCmd = "/home/hadoop/startvideo.sh "+ savedir+ file.getName() + " " + file.getName() + " " + port + "> /tmp/vlc.log 2>&1 &";
		  //ShellExec.executeShell("/home/hadoop/startvideo.sh /home/hadoop/dfs/user/hadoop/test-in/daomeixiong001.mp4 test.dsp");
	      Log.op(shellCmd);
	      ShellExec.executeShell(shellCmd);		
	    }  
	    catch(Exception ex)  
	    {  
	      throw new RuntimeException(ip+"Exceptio");  
	    }  
	  }  
	    
	  public void writeOutInfo(Socket sock,String infoStr)throws Exception//����Ϣ��!������  
	  {  
	    OutputStream sockOut = sock.getOutputStream();  
	    sockOut.write(infoStr.getBytes());  
	  }  
	    
	  public File getClientFileName(InputStream sockIn) throws Exception //��ȡ�ļ�����  
	  {    
	    //��ȡ�ͻ��������͵��ļ���,���ж���D�̴���ͬ���ļ������  
	    byte[] bufName=new byte[1024];  
	    int lenInfo =0;  
	    lenInfo = sockIn.read(bufName);  //��ȡ�ļ���  
	    String fileName = new String(bufName,0,lenInfo);  
	    
	    //FileDir  filedir = new FileDir(fileName);
	    /* 
	    File dir = new File("/tmp/");    
	  
	    File[] files=dir.listFiles();   //����d��Ŀ¼  
	    for(File f:files)  
	    {  
	      if(!f.isDirectory())  //������ĸ��ļ�����һ��Ŀ¼�Ļ�  
	      {   
	        if(f.getName().equals(fileName))  //�ж��Ƿ���ͬ���ļ�  
	        {  
	          Log.op(f.getName()+"�ļ��Ѵ���,�Ͽ���ipl��."+System.getProperty("line.separator"));  
	          writeOutInfo(sock,"������Ѵ���ͬ���ļ�!"); //��!��ͻ��˵���Ϣ  
	          return null;         
	        }  
	      }  
	    }  */
	    Log.op("Get filename "+ fileName);       
	    File file= new File("/tmp/"+fileName);    
	    if(file.createNewFile())  
	    {   
	      Log.op("creat "+fileName+" success");  
	      writeOutInfo(sock,"FileSendNow");    //���߿ͻ���,��ʼ������ݰ�    
	      return file;  
	    }  
	    else  
	    {  
	      Log.op("creat "+fileName+" failed"); 
	      return null; 
	    }  
	  }  
}
